// param [{lon:123,lat:22},{}...]
//聚散点效果
function MapCluster(points,points2){
	//配置:
	
	var areaWidth = 100;//选取的聚散点的宽度和高度
	var areaHeight = 100;
	
	var graph = '/resources/img/clusterPoint/redCircle.png';//显示点的图片的来源
	var graph2 = '/resources/img/clusterPoint/redRing.png';
	
	
	var graphStartWidth = 30;//本缩放程度下，数据最小的点的宽度和高度
	var graphStartHeight = 30;
	//数据点绘制时，按照数据分级，不同级别图片大小不同
	
	var levelFromZero = false;//默认按照从0到当前数据最大进行分级。值为false时，代表从当前数据最大值和最小值之间进行分级。
	
	var graphLevelNum = 6;//从0到最大数据分级的个数
	
	var graphStepWidth = 20;//每增加一级增加的宽和高
	var graphStepHeight = 20;//每增加一级增加的宽和高
	
	var pointLayer = mapClusterLayer;
	var tipLayer = mapClusterLayer;
	var fontColor = 'pink';
	var tips = [];
	
	this.show = function(){
		pointLayer.removeAllFeatures();
		tipLayer.removeAllFeatures();
		genPoints();
		return this;
	};
	this.remove = function(){
		pointLayer.removeAllFeatures();
		for(var i in tips){
			tips[i].remove();
		}
		return this;
	};
	this.setAreaWidth = function(num){
		areaWidth = num;
		return this;
	};
	this.setAreaHeight = function(num){
		areaHeight = num;
		return this;
	};
	this.setGraph = function(link){
		graph = link;
		return this;
	};
	this.setGraphStartWidth = function(num){
		graphStartWidth = num;
		return this;
	};
	this.setGraphStartHeight = function(num){
		graphStartHeight = num;
		return this;
	};
	this.setLevelFromZero = function(trueOrFalse){
		levelFromZero = trueOrFalse;
		return this;
	};
	this.setGraphLevelNum = function(num){
		graphLevelNum = num;
		return this;
	};
	this.setLevelFromZero = function(trueOrFalse){
		levelFromZero = trueOrFalse;
		return this;
	};
	this.setGraphStepWidthAndHeight = function(widthNum,heightNum){
		graphStepWidth = widthNum;
		graphStepHeight = heightNum;
		return this;
	};
	this.setPointLayer = function(layer){
		pointLayer = layer;
		return this;
	};
	function genPoints(){
		var areas = getMapAreas();
		//计算出每个区域内的点数
		for(var i in points){
			var p = points[i];
			for(var j in areas){
				var a = areas[j];
				//左边界和上边界上的点被包含在范围内
				if(p[0]>=a.left && p[0]<a.right && p[1]<=a.top && p[1]>a.bottom){
					a.count ++;
					a.lonSum += p[0];
					a.latSum += p[1];
					break;
				}
			}
		}
		if(points2){
			for(var i in points2){
				var p = points2[i];
				for(var j in areas){
					var a = areas[j];
					//左边界和上边界上的点被包含在范围内
					if(p[0]>=a.left && p[0]<a.right && p[1]<=a.top && p[1]>a.bottom){
						a.count2 ++;
						a.lonSum2 += p[0];
						a.latSum2 += p[1];
						break;
					}
				}
			}
		}
		var areaPoints = getAreaPoints(areas);
		pointLayer.addFeatures(areaPoints);
		pointLayer.setVisibility(true);
		
	}
	function getAreaPoints(areas){
		var features = [];
		var levels = getLevels(areas);
		var tipDatas = [];
		for(var i in areas){
			var a = areas[i];
			if(a.count>0){
				var level = getLevelByCount(a.count,levels);
				if(level){
					var lon = a.lonSum/a.count;
					var lat = a.latSum/a.count;
					var point = new SuperMap.Geometry.Point(lon,lat);
					var style = {
							graphic:true,
							externalGraphic:graph,
							graphicWidth:level.graphicWidth,
							graphicHeight:level.graphicHeight,
							label:a.count>1?a.count+"":"",
							fontColor:fontColor
					};
					var vector = new SuperMap.Feature.Vector(point,null,style);
					if(a.count2>0){
						tipDatas.push([lon,lat,a.count2]);
					}
					features.push(vector);
				}
			}else if(a.count2>0){
				var lon = a.lonSum2/a.count2;
				var lat = a.latSum2/a.count2;
				var point = new SuperMap.Geometry.Point(lon,lat);
				var style = {
						graphic:true,
						externalGraphic:graph2,
						graphicWidth:graphStartWidth/2,
						graphicHeight:graphStartHeight/2,
					//	label:"无气象数据",
					//	fontColor:fontColor
				};
				var vector = new SuperMap.Feature.Vector(point,null,style);
				if(a.count2>0){
					tipDatas.push([lon,lat,a.count2]);
				}
				features.push(vector);
			}
		}
		tipDatas.sort(sortTipDatas);
		tips = [];
		for(var i=0;i<tipDatas.length;i++){
			var tip = new drawMapClusterTip(tipDatas[i][0],tipDatas[i][1],'相关故障  '+tipDatas[i][2]+'  次');
			if(i+1>tipDatas.length/2){
				tip.isTipTop(true);
			}else{
				tip.isTipTop(false);
			}
			tip.show();
			tips.push(tip);
		}
		return features;
	}
	function sortTipDatas(obj1,obj2){
		return obj1[1] - obj2[1];
	}
	function getLevelByCount(count,levels){
		for(var i in levels){
			if(count<=levels[i].count){
				return levels[i];
			}
		}
		return levels[0];
	}
	function getLevels(areas){
		var levels = [];
		var min = 0;
		var max = 0;
		if(! levelFromZero){
			min = areas[0].count;
			for(var i in areas){
				var a = areas[i];
				max = a.count>max?a.count:max;
				min = a.count<min?a.count:min;
			}
		}else{
			for(var i in areas){
				max = areas[i].count>max?areas[i].count:max;
			}
		}
		var countStep = (max-min)/graphLevelNum;
		for(var i=0;i<graphLevelNum;i++){
			levels.push({
				count:Math.ceil(min+(i+1)*countStep),
				graphicWidth:Math.ceil(graphStartWidth+(graphStepWidth*i)),
				graphicHeight:Math.ceil(graphStartHeight+(graphStepHeight*i))
			});
		}
		return levels;
	}
	function getMapAreas(){
		//将地图按照px分块，然后转为经纬度。
		var mapSize = map.getSize();//{w:123,h:123}
		//根据px从左上角到右下角分px块，然后转化为lon，lat
		var areas = [];//[{bottom,left,right,top},{}...]
		for(var i=0;i < mapSize.w;i+=areaWidth){
			var leftPx = i;
			var rightPx = leftPx+areaWidth > mapSize.w?mapSize.w:leftPx+areaWidth;
			for(var j=0;j < mapSize.h ; j+=areaHeight){
				var topPx = j;
				var bottomPx = topPx+areaHeight > mapSize.h?mapSize.h:topPx+areaHeight;
				var leftTop = map.getLonLatFromPixel(new SuperMap.Pixel(leftPx,topPx));
				var rightBottom = map.getLonLatFromPixel(new SuperMap.Pixel(rightPx,bottomPx));
				areas.push({bottom:rightBottom.lat,left:leftTop.lon,right:rightBottom.lon,top:leftTop.lat,count:0,lonSum:0,latSum:0,count2:0,lonSum2:0,latSum2:0});
			}
		}
		//bottom:下纬度，left:左经度，right：右经度，top：上纬度
		return areas;
	}
	
}

//画一个线，画一个框，里面填字。线：黑色虚线。框：实心框，圆角，底色
function drawMapClusterTip(lon,lat,content){
	
	var tipTop = true;
	
	var lineLengthFixed = true;//引线是否定长
	var lineLength = 100;//如果定长，长度为...
	
	//box
	var marginTop = 5;
	var marginBottom = 105;
	var marginLeft = 5;
	var marginRight = 5;
	
	var maxHeight = 50;
	var maxWidth = 100;
	var padding = 10;
	var boxBorderStyle = 'dot';
	var boxFillColor = 'rgba(195,179,179,0.5)';
	var boxBorderColor = 'gray';
	
	//text
	var fontSize = 16;
	var fontColor = 'red';
	
	var lineStyle = 'dot';
	var lineColor = 'gray';
	var lineWidth = 1;
	
	var layer = mapClusterLayer;
	var lineVector = undefined;
	var pop = undefined;
	this.show = function(){
		var lineLonLat = getLineLonLat();
		drawLine(lon,lat,lineLonLat.lon,lineLonLat.lat);
		drawBox(lineLonLat);
		layer.setVisibility(true);
		return this;
	};
	this.isTipTop = function(trueOrFalse){
		tipTop = trueOrFalse;
		return this;
	};
	this.remove = function(){
		layer.removeFeatures([lineVector]);
	//	pop.remove();
		return this;
	};
	function getLineLonLat(){
		var lineLonLat = undefined;
		var pixel = map.getPixelFromLonLat(new SuperMap.LonLat(lon,lat));
		if(lineLengthFixed){
			if(tipTop){
				var y = pixel.y - lineLength;
				y = y>marginTop+maxHeight?y:marginTop+maxHeight;
				lineLonLat = map.getLonLatFromPixel(new SuperMap.Pixel(pixel.x,y));
			}else{
				var mapSize = map.getSize();//{w:123,h:123}
				var y = pixel.y + lineLength;
				y = y>mapSize.h-marginBottom-maxHeight?mapSize.h-marginBottom-maxHeight:y;
				lineLonLat = map.getLonLatFromPixel(new SuperMap.Pixel(pixel.x,y));
			}
		}else{
			if(tipTop){
				var y = marginTop+maxHeight;
				lineLonLat = map.getLonLatFromPixel(new SuperMap.Pixel(pixel.x,y));
			}else{
				var mapSize = map.getSize();//{w:123,h:123}
				var y = mapSize.h - marginBottom - maxHeight;
				lineLonLat = map.getLonLatFromPixel(new SuperMap.Pixel(pixel.x,y));
			}
		}
		return lineLonLat;
	}
	function drawLine(lon1,lat1,lon2,lat2){
		var points = [];
		points.push( new SuperMap.Geometry.Point(lon1,lat1));
		points.push( new SuperMap.Geometry.Point(lon2,lat2));
		var line = new SuperMap.Geometry.LineString(points);
		lineVector = new SuperMap.Feature.Vector(line);
		lineVector.style = {
			strokeColor : lineColor,
			strokeDashstyle:lineStyle,
			strokeWidth : lineWidth
		};
		layer.addFeatures([lineVector]);
	}
	function drawBox(lineLonLat){
		var linePixel = map.getPixelFromLonLat(lineLonLat);
		//拿到px，计算文本宽度和高度，画出box
		var boxSize = caculateBoxSize(content);
		
		var x;
		var y;
		
		x = linePixel.x - boxSize.w/2;
		if(tipTop){
			y = linePixel.y;
		}else{
			y = linePixel.y + boxSize.h;
		}
		var lonlat = map.getLonLatFromPixel(new SuperMap.Pixel(x,y));//左下角
		var lonlat2 = map.getLonLatFromPixel(new SuperMap.Pixel(x+boxSize.w,y-boxSize.h));//右上角
		var rectangle = new SuperMap.Geometry.Rectangle(lonlat.lon, lonlat.lat, lonlat2.lon-lonlat.lon, lonlat2.lat-lonlat.lat);
		var rectangleVector = new SuperMap.Feature.Vector(rectangle);
		rectangleVector.style = {
				label:content,
				strokeColor:boxBorderColor,
				fillColor:boxFillColor,
				fontColor:fontColor,
				strokeDashstyle:boxBorderStyle
		};
		layer.addFeatures([rectangleVector]);
		/*pop = new HoverPopup(content);
		pop.setLonLat(lon, lat);
		pop.setKeepInMap(false);
		pop.show();
		var popup = pop.getPopup();
		var popSize = popup.size;
		var popupDiv = $(popup.div);*/
		
		/*var x = linePixel.x - popSize.w/2;
		if(x<0){
			x = marginLeft;
		}
		if(x+popSize.w+marginRight>mapSize.w){
			x = mapSize.w - popSize.w - marginRight;
		}
		var y = linePixel.y;
		if(tipTop){
			y = linePixel.y - popSize.h;
		}
	//	popupDiv.css('left',x+'px');
		//popupDiv.css('top',y+'px');
		var lonlat = map.getLonLatFromPixel(new SuperMap.Pixel(x,y));
		popup.lonlat = lonlat;
		popup.updateSize();*/
	}
	function caculateBoxSize(content){
		var div = $('#hiddenDiv');
		div.html(content);
		return {h:div.height(),w:div.width()};
	}
}